var app = getApp();

import {
  uploadMedia
} from "../../api/common.js";

Page({
  data: {
    formats: {},
    readOnly: false,
    placeholder: '开始输入...',
    editorHeight: 300,
    keyboardHeight: 0,
    isIOS: false,
    placeholder: '请输入文章内容...',
    placeholders: '输入公众号链接...'
  },
  readOnlyChange() {
    this.setData({
      readOnly: !this.data.readOnly
    })
  },
  onLoad() {
    const platform = wx.getSystemInfoSync().platform
    const isIOS = platform === 'ios'
    this.setData({
      isIOS
    })
    const that = this
    this.updatePosition(0)
    let keyboardHeight = 0
    wx.onKeyboardHeightChange(res => {
      if (res.height === keyboardHeight) return
      const duration = res.height > 0 ? res.duration * 1000 : 0
      keyboardHeight = res.height
      setTimeout(() => {
        wx.pageScrollTo({
          scrollTop: 0,
          success() {
            that.updatePosition(keyboardHeight)
            that.editorCtx.scrollIntoView()
          }
        })
      }, duration)

    })

  },


  /**
   * 关闭评论框
   */

  closeCommentary: function () {
    this.setData({
      showTextarea: false
    });
  },



  /***
   * 获取用户信息
   */

  getUserInfoFun: function(e) {
    // console.log(e);
    if (e.detail.errMsg == "getUserInfo:ok") {
      wx.setStorageSync('scope', true);
      this.getProfile();
      wx.showLoading({
        title: '登陆中...',
      })
    } else {
      return
    }


  },


  bindWordLimit: function(e) {
    this.setData({
      link: e.detail.value,
    })
  },

  getPostinfo: function() {

    this.setData({
      showTextarea: true,
    })

  },





  input: function(e) {
    app.datalist.detail =e.detail.html

  },

  onShow: function() {
    let user = app.globalData.user
    if (!user) {
      wx.showToast({
        title: '未登录,请登录',
      })
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
    this.setData({
      user: user,
    })
  },

  jumpAnalysis: function() {
    wx.navigateTo({
      url: '/pages/posts/addposts',
    })
  },
  updatePosition(keyboardHeight) {
    const toolbarHeight = 50
    const {
      windowHeight,
      platform
    } = wx.getSystemInfoSync()
    let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight) : windowHeight
    this.setData({
      editorHeight,
      keyboardHeight
    })
  },
  calNavigationBarAndStatusBar() {
    const systemInfo = wx.getSystemInfoSync()
    const {
      statusBarHeight,
      platform
    } = systemInfo
    const isIOS = platform === 'ios'
    const navigationBarHeight = isIOS ? 44 : 48
    return statusBarHeight + navigationBarHeight
  },
  onEditorReady() {
    const that = this
    wx.createSelectorQuery().select('#editor').context(function(res) {
      that.editorCtx = res.context
    }).exec()

  },
  blur() {
    this.editorCtx.blur()
  },
  format(e) {
    let {
      name,
      value
    } = e.target.dataset
    if (!name) return
    // console.log('format', name, value)
    this.editorCtx.format(name, value)

  },
  onStatusChange(e) {
    const formats = e.detail
    this.setData({
      formats
    })
  },
  insertDivider() {
    this.editorCtx.insertDivider({
      success: function() {
        console.log('insert divider success')
      }
    })
  },
  clear() {
    this.editorCtx.clear({
      success: function(res) {
        console.log("clear success")
      }
    })
  },
  removeFormat() {
    this.editorCtx.removeFormat()
  },
  insertDate() {
    const date = new Date()
    const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
    this.editorCtx.insertText({
      text: formatDate
    })
  },
  insertImage() {
    const that = this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        wx.showLoading({
          title: '上传中',
        })
         uploadMedia(res.tempFilePaths[0]).then(res => { // 上传到本地媒体库
          that.editorCtx.insertImage({
            src: res.url, //上传本地为res.url
            data: {
              id: 'abcd',
              role: 'god'
            },
            width: '80%',
            success: function() {
              wx.hideLoading();
              wx.showToast({
                title: '上传成功',
              })
              console.log('insert image success')
            }
          })
        })
      }
    })
  }
})